﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Revit;

namespace YArchitech.Public
{
	public class DesignNoteHelper
	{
		public DesignNoteHelper(UIApplication uiApp, string strPath)
		{
			this.uiApp = uiApp;
			this.strPath = strPath;
		}

        public Autodesk.Revit.DB.View CopyDesignNote()
		{
            Autodesk.Revit.DB.View view = null;
			Document document = this.uiApp.ActiveUIDocument.Document;
			view = this.NewDesignNote(document);
			this.sourceDoc = this.uiApp.Application.OpenDocumentFile(this.strPath);
			if (this.sourceDoc == null)
			{
				return null;
			}
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(this.sourceDoc);
			ICollection<Element> collection = filteredElementCollector.OfClass(typeof(ViewPlan)).ToElements();
			foreach (Element element in collection)
			{
                Autodesk.Revit.DB.View view2 = (Autodesk.Revit.DB.View)element;
                if ((int)view2.ViewType == 1)
				{
					this.sourceView = view2;
					break;
				}
			}
			this.transform = Transform.Identity;
			FilteredElementCollector filteredElementCollector2 = new FilteredElementCollector(this.sourceDoc, this.sourceView.Id);
			ICollection<Element> collection2 = filteredElementCollector2.OfClass(typeof(TextNote)).ToElements();
			foreach (Element element2 in collection2)
			{
				this.elements.Add(element2.Id);
			}
			FilteredElementCollector filteredElementCollector3 = new FilteredElementCollector(this.sourceDoc, this.sourceView.Id);
			ICollection<Element> collection3 = filteredElementCollector3.OfClass(typeof(CurveElement)).ToElements();
			foreach (Element element3 in collection3)
			{
				this.elements.Add(element3.Id);
			}
			if (this.elements.Count<ElementId>() != 0)
			{
				RevitVersionFuncs.CopyElements(this.sourceView, this.elements, view, this.transform);
			}
			this.CloseSourceDocument();
			return view;
		}

        public Autodesk.Revit.DB.View CustomDesignNote()
		{
			RichTextBox richTextBox = new RichTextBox();
			richTextBox.LoadFile(this.strPath, RichTextBoxStreamType.RichText);
			Document document = this.uiApp.ActiveUIDocument.Document;
            Autodesk.Revit.DB.View view = this.NewDesignNote(document);
			XYZ viewDirection = view.ViewDirection;
			XYZ upDirection = view.UpDirection;
            TextAlignFlags textAlign = (TextAlignFlags)576;
			string text = richTextBox.Text;
			if (this.strPath != "")
			{
				for (int i = 0; i < text.Length / 5000; i++)
				{
					text = text.Insert((i + 1) * 5000, "!--");
				}
			}
			string[] array = Regex.Split(text, "!--");
			if (string.IsNullOrWhiteSpace(text) || string.IsNullOrEmpty(text))
			{
				throw new ArgumentException("导入文档为空");
			}
			int num = 0;
			foreach (string text2 in array)
			{
				if (text2 != "!--")
				{
					XYZ origin = new XYZ((double)(100 * num), 0.0, 0.0);
					if (!string.IsNullOrEmpty(text2) && !string.IsNullOrWhiteSpace(text2))
					{
						document.CreateTextNoteAutoWrapping(view, origin, viewDirection, upDirection, textAlign, text2, 50);
					}
					document.Regenerate();
					num++;
				}
			}
			return view;
		}

        private Autodesk.Revit.DB.View NewDesignNote(Document doc)
		{
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(doc);
			filteredElementCollector.OfClass(typeof(ViewDrafting));
			foreach (Element element in filteredElementCollector)
			{
				if (element.Name == "设计说明")
				{
					doc.DeleteElement(element);
					break;
				}
			}
			ViewDrafting viewDrafting = RevitViewExtension.CreateViewDrafting(doc);
			viewDrafting.ViewName = "设计说明";
			viewDrafting.GetParameter(BuiltInParameter.VIEW_SCALE_PULLDOWN_METRIC).Set(100);
			return viewDrafting;
		}

		private void CloseSourceDocument()
		{
			if (this.sourceDoc != null)
			{
				this.sourceDoc.Close(false);
			}
		}

		private UIApplication uiApp;

		private string strPath = "";

		private Document sourceDoc;

		private IList<ElementId> elements = new List<ElementId>();

		private Transform transform;

        private Autodesk.Revit.DB.View sourceView;
	}
}
